﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
//using Kiiro.Model.Utility;

namespace Kiiro.Model.EventHandlers.ItemEventHandlers
{
    public class CustomItemEventReceiver : SPItemEventReceiver
    {
        public override void ItemAdded ( SPItemEventProperties properties )
        {
            Guid siteCollectionId = new Guid ( );
            string serverRelativeUrl = null;

            using ( SPWeb web = properties.ListItem.ParentList.ParentWeb )
            {
                siteCollectionId = web.Site.ID;
                serverRelativeUrl = web.ServerRelativeUrl;
            }

            ActivityHistory activity = PopulateCustomItemHistory ( properties.ListItem, siteCollectionId, serverRelativeUrl );

            if ( activity != null )
            {
                ActivityHistoryModel.Save ( activity, siteCollectionId, serverRelativeUrl );
            }         

            base.ItemAdded ( properties );
        }

        public override void ItemUpdated ( SPItemEventProperties properties )
        {
            base.ItemUpdated ( properties );
        }

        private ActivityHistory PopulateCustomItemHistory ( SPListItem listItem, Guid siteCollectionId, string serverRelativeUrl )
        {
            ActivityHistory activity = new ActivityHistory ( );                       

            User user = SPDataHelpers.GetMidoriUser ( listItem, "Author" );
            
            using(SPWeb web = SPHelpers.GetPortalWeb(siteCollectionId, serverRelativeUrl))
            {                
                SPUser spUser = web.AllUsers.GetByID(user.Id);
                user = UserModel.GetUserByLogin(spUser.LoginName, siteCollectionId, serverRelativeUrl);
                activity.CreatedBy = user;
                if (listItem["Author"] != listItem["Editor"])
                {
                    user = SPDataHelpers.GetMidoriUser(listItem, "Editor");
                    spUser = web.AllUsers.GetByID(user.Id);
                    user = UserModel.GetUserByLogin(spUser.LoginName, siteCollectionId, serverRelativeUrl);                    
                }
                activity.ModifiedBy = user;
            }

            activity.CreatedDate = DateTime.Parse ( listItem [ "Created" ].ToString ( ) );            
            activity.ModifiedDate = DateTime.Parse ( listItem [ "Modified" ].ToString ( ) );
            activity.Title = listItem.Title;

            activity.ActivityItemType = ItemType.CustomItem;
            activity.ActivityReferenceUrl = string.Format ( "{0}/{1}", serverRelativeUrl, listItem.Url );

            if ( listItem.ParentList.ContentTypes.Count > 0 )
                activity.Description = listItem.ParentList.ContentTypes [ 0 ].Name;

            activity.EditType = EditType.Created;

            return activity;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Model/EventHandlers/ItemEventHandlers/CustomItemEventReceiver.cs $
 * 
 * 7     30/09/09 9:24a Shafia
 * 
 * 6     11/09/09 4:40p Shafia
 * 
 * 5     9/03/09 11:47a Stefanf
 * 
 * 4     9/02/09 9:36a Stefanf
 * 
 * 3     17/08/09 10:29a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/